
function mapValue(x, a, b, c, d) {
    return (x - a) * (d - c) / (b - a) + c;
}

class Camera{
    constructor() {
        this._viewMat = cc.mat4();
        this._viewMatInv = cc.mat4();

        this._projMat = cc.mat4();

        this._pvMat = cc.mat4();
        this._pvMatInv = cc.mat4();

        this._bUpdate = false;

        this._frustum = cc.geometry.frustum.create();
        this._frustum.accurate = true;

        this._near = 0.1;
        this._far = 100;
    }

    lookAt(pos,center,up){
        cc.Mat4.lookAt(this._viewMat,pos,center,up);
        cc.Mat4.invert(this._viewMatInv,this._viewMat);
        this._bUpdate = true;
    }

    getPos(outVec3){
        cc.Mat4.getTranslation(outVec3,this._viewMatInv);
        return outVec3;
    }

    project(fov,aspect,near,far){
        cc.Mat4.perspective(this._projMat,fov,aspect,near,far);
        this._near = near;
        this._far = far;
    }

    /**
     *
     * @return {boolean} true 表示有更新
     */
    update(){
        if(this._bUpdate){
            cc.Mat4.multiply(this._pvMat,this._projMat,this._viewMat);
            cc.Mat4.invert(this._pvMatInv,this._pvMat);
            this._frustum.update(this._pvMat,this._pvMatInv);
            this._bUpdate = false;
            return true;
        }
        return false;
    }

    getViewMat(){
        return this._viewMat;
    }

    getProjViewMat(){
        return this._pvMat;
    }

    getProjMat(){
        return this._projMat;
    }

    // calZ(z){
    //     let n = mapValue(z,-1/this._near,-1/this._far,-1,1);
    //     if(n === 0){
    //         return -Infinity;
    //     }
    //     return 1/n;
    // }

    getAmbientLight(){
        return this._ambLight;
    }
    getDiffuseLight(){
        return this._diffLight;
    }

    setAmbientLight(ambientLight){
        this._ambLight = ambientLight;
    }

    setDiffuseLight(diffuseLight){
        this._diffLight = diffuseLight;
    }


    // TODO: 添加一些camera进行移动的方法???

}
export default Camera;
